package com.thtf.homework.api;

import com.thtf.homework.dto.DataSourceDTO;
import com.thtf.homework.model.DataSource;
import com.thtf.homework.model.Result;
import com.thtf.homework.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/datasource")
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    // 数据源：创建
    @PostMapping("/create")
    public Result createDataSource(@RequestBody DataSourceDTO request) {
        // 参数校验
        if (request.getDataSourceStruct().isEmpty() || request.getDataSourceType().isEmpty() ||
                request.getDataSourceClassification().isEmpty() || request.getDataSourceName().isEmpty() ||
                request.getDataSourceURL().isEmpty() || request.getDataSourceUsername().isEmpty() ||
                request.getDataSourcePassword().isEmpty() || request.getDataSourceMode().isEmpty()) {
            System.out.println(request);
            return Result.error("创建数据源失败（必填参数含空）");

        } else {
            System.out.println(request);

            dataSourceService.create(request.getDataSourceStruct(), request.getDataSourceType(),
                    request.getDataSourceClassification(), request.getDataSourceName(),
                    request.getDataSourceURL(), request.getDataSourceUsername(),
                    request.getDataSourcePassword(), request.getDataSourceMode(),
                    request.getDataSourceDescription());

            return Result.success();
        }
    }

    //  数据源:修改
    @PostMapping("/update")
    public Result updateDataSource(@RequestBody DataSourceDTO request) {
        if (request.getDataSourceId().isEmpty()) {
            return Result.error("修改数据源失败(参数为空)");
        } else {
            dataSourceService.update(request.getDataSourceClassification(), request.getDataSourceName(),
                    request.getDataSourceURL(), request.getDataSourceUsername(),
                    request.getDataSourcePassword(), request.getDataSourceMode(),
                    request.getDataSourceDescription(), request.getDataSourceId());

            return Result.success();
        }
    }

    // 数据源：查询
    @GetMapping("/query")
    public Result<List<DataSource>> queryDataSource(String dataSourceClassification) {
        // 根据数据源分类（所在数据目录、目录名称），查询数据源信息
        // 参数校验
        if (dataSourceClassification != null) {
            //查询
            List<DataSource> dataSource = dataSourceService.query(dataSourceClassification);
            return Result.success(dataSource);

        } else {
            return Result.error("查询数据源失败(参数为空)");
        }
    }

    @GetMapping("/delete")
    public Result deleteDataSource(String dataSourceId) {
        if (dataSourceId.isEmpty()) {
            return Result.error("删除数据源失败（参数为空）");
        } else {
            dataSourceService.delete(dataSourceId);
            return Result.success();
        }
    }
}
